package com.tz.actions.user;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.tz.entity.User;
import com.tz.service.IUserService;
import com.tz.util.MyAcUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

import java.util.Map;


@Namespace("/")
public class UserLoginAction extends ActionSupport {

	private String username;
	private String password;
	private boolean info;



	private IUserService userService =
			MyAcUtil.getInstance().getBean(IUserService.class);

	public void validateExecute(){

		if(StringUtils.isBlank(username)){
			addFieldError("username","用户名不能为空！");
		}
		if(StringUtils.isBlank(password)){
			addFieldError("password","密码不能为空！");
		}


	}


	@Action(value = "login",
			results = {@Result(type = "redirectAction",location = "index"),
			@Result(name = "input",location = "/myaccount.jsp")
			})
	public String execute() {

			User user = userService.login(username, password);
			if(user==null){
				addFieldError("login","用户名或密码错误");
			}

		ActionContext context=ActionContext.getContext();
		Map<String,Object> session=context.getSession();
		session.put("user",user);

			return SUCCESS;

	}





	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public boolean isInfo() {
		return info;
	}

	public void setInfo(boolean info) {
		this.info = info;
	}
}
